<?php
/**
 * 测试控制器
 */

namespace app\api\controller;

use app\api\exception\ApiServiceException;
use app\api\service\TestService;
use app\common\model\OfferDetail;
use app\common\model\Send;
use app\common\validate\TestValidate;
use think\response\Json;

class TestController extends ApiBaseController
{

    protected array $loginExcept = [
        'api/test/index',
    ];
    public function index()
    {
        // $start_end = getstart();
        // $start     = $start_end['start'];
        // $end       = $start_end['end'];
        // print_r($start);
        // print_r($end);
        // $list = OfferDetail::select();
        // foreach ($list as $key => $v) {

        // 	$send = Send::where('offer_detail_id', $v['id'])->select();

        // 	foreach ($send as $key => $value) {
        // 		$send_detail = $this->array_do5($value['detail'], $v['detail']);
        // 		foreach ($send_detail as $dk => $dv) {
        // 			$send_detail[$dk]['money'] = round($dv['price'] * $dv['num'], 2);
        // 		}

        // 		$send_money = array_sum(array_column($send_detail, 'money'));
        // 		Send::where('id', $value['id'])->update(['detail' => $send_detail, 'money' => $send_money, 'send_time' => strtotime($value['send_time'])]);

        // 	}
        // }
    }
    public function array_do5($array1, $array2)
    {
        $result = [];
        foreach ($array2 as $item2) {
            foreach ($array1 as $item1) {
                if ($item1['type_id'] == $item2['type_id']) {
                    $item1['num'] = $item2['num'];
                    $result[]     = $item1;
                    break;
                }
            }
        }
        return $result;
    }
    /**
     * 列表
     * @param TestService $service
     * @return Json
     */
    public function index2(TestService $service)
    {
        $list = Send::field('school_id,send_time')->group('school_id,send_time')->select()->toArray();
        foreach ($list as $key => $value) {
            $send        = Send::where('school_id', $value['school_id'])->where('send_time', strtotime($value['send_time']))->select()->toArray();
            $detail      = array_column($send, 'detail');
            $twoDimArray = [];

            // 遍历三维数组，将其转换为二维数组
            foreach ($detail as $subArray) {
                foreach ($subArray as $item) {
                    $twoDimArray[] = $item;
                }
            }
            $detail = $this->array_do($twoDimArray);

            $offer_detail = OfferDetail::where('school_id', $value['school_id'])->where('send_time', strtotime($value['send_time']))->findOrEmpty();
            if (! $offer_detail->isEmpty()) {
                $offer_detail->save(['detail' => $detail]);
            }
        }
        echo 'success';
    }
    public function array_do($array)
    {
        $result = [];

        foreach ($array as $item) {
            $type_id = $item['type_id'];
            if (! isset($result[$type_id])) {
                $result[$type_id] = $item;
            } else {
                $result[$type_id]['num'] += $item['num'];
            }
        }

        $result = array_values($result);
        return $result;
    }
    public function array_do2($data)
    {
        $groupedData = [];

        foreach ($data as $item) {
            $school_id = $item['school_id'];
            $type_id   = $item['type_id'];
            $spec_id   = $item['spec_id'];
            $brand_id  = $item['brand_id'];
            $order_id  = $item['order_id'];
            $key       = $school_id . '-' . $type_id;

            if (! isset($groupedData[$key])) {
                $groupedData[$key] = [
                    'school_id' => $school_id,
                    'type_id'   => $type_id,
                    'spec_id'   => $spec_id,
                    'brand_id'  => $brand_id,
                    'order_id'  => $order_id,
                    'total_num' => 0,
                ];
            }

            $groupedData[$key]['total_num'] += $item['num'];
        }
        $result = array_values($groupedData);
        return $result;
    }
    /**
     * 添加
     *
     * @param TestValidate $validate
     * @param TestService $service
     * @return Json
     */
    public function add(TestValidate $validate, TestService $service): Json
    {
        $check = $validate->scene('api_add')->check($this->param);
        if (! $check) {
            return api_error($validate->getError());
        }

        $result = $service->createData($this->param);

        return $result ? api_success() : api_error();
    }

    /**
     * 详情
     *
     * @param TestValidate $validate
     * @param TestService $service
     * @return Json
     */
    public function info(TestValidate $validate, TestService $service): Json
    {
        $check = $validate->scene('api_info')->check($this->param);
        if (! $check) {
            return api_error($validate->getError());
        }

        try {

            $result = $service->getDataInfo($this->id);
            return api_success([
                'user_level' => $result,
            ]);

        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }

    /**
     * 修改
     * @param TestService $service
     * @param TestValidate $validate
     * @return Json
     */
    public function edit(TestService $service, TestValidate $validate): Json
    {
        $check = $validate->scene('api_edit')->check($this->param);
        if (! $check) {
            return api_error($validate->getError());
        }

        try {
            $service->updateData($this->id, $this->param);
            return api_success();
        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }

    /**
     * 删除
     * @param TestService $service
     * @param TestValidate $validate
     * @return Json
     */
    public function del(TestService $service, TestValidate $validate): Json
    {
        $check = $validate->scene('api_del')->check($this->param);
        if (! $check) {
            return api_error($validate->getError());
        }

        try {
            $service->deleteData($this->id);
            return api_success();
        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }
}
